﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Security.Cryptography;
using System.IO;
using System.Text;

using CE.User;
using CE.Global;
using CE.Database;
using CE.Provider;
using CE.TravelInsurance;
using CE.Company;
using CE.Mail;
using CE.InputOut;
using CE.Image;

public partial class subscribe_email : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["ImageCount"] != null)
        {
            if (Convert.ToInt32(Session["ImageCount"].ToString()) > 5)
            {
                lblMessage.Text = "Please try after a few moment! You are session has been locked!";
                btnEmailThis.Enabled = false;
                btnNewImage.Enabled = false;
                txtEmail.Enabled = false;
                txtSecurityCode.Enabled = false;
                //Response.Redirect("usermessage.aspx?mt=um&ec=1993");
            }
            else
            {
                lblMessage.Text = "";
                btnEmailThis.Enabled = true;
                btnNewImage.Enabled = true;
                txtEmail.Enabled = true;
                txtSecurityCode.Enabled = true;
            }
        }


        if (!Page.IsPostBack)
        {
            rfvtxtFriendName2.Enabled = false;
            rfvtxtFriendName3.Enabled = false;
            rfvtxtFriendName4.Enabled = false;
            rfvtxtFriendName5.Enabled = false;

            rfvtxtEmailFriend2.Enabled = false;
            rfvtxtEmailFriend3.Enabled = false;
            rfvtxtEmailFriend4.Enabled = false;
            rfvtxtEmailFriend5.Enabled = false;

            if (Session["ImageCount"] != null)
            {
                if (Convert.ToInt32(Session["ImageCount"].ToString()) <= 5)
                {
                    CreateUpdateImageAndSession();
                }
            }
            else
            {
                CreateUpdateImageAndSession();
            }

            
        }

    }

    protected void btnEmailThis_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (Session[CE.Global.General.SecurityCheckWord] != null)
            {
                string _usrText = txtSecurityCode.Text.ToUpper();
                string _imgText = Session[CE.Global.General.SecurityCheckWord].ToString().ToUpper();

                if (_usrText == _imgText)
                {
                    if (SendMail(txtEmailFriend1.Text, txtFriendName1.Text))
                    {
                        TAUserOptionLog(txtEmailFriend1.Text, txtFriendName1.Text);

                        if (txtEmailFriend2.Text.Trim() != "")
                        {
                            SendMail(txtEmailFriend2.Text, txtFriendName2.Text);
                            TAUserOptionLog(txtEmailFriend2.Text, txtFriendName2.Text);
                        }

                        if (txtEmailFriend3.Text.Trim() != "")
                        {
                            SendMail(txtEmailFriend3.Text, txtFriendName3.Text);
                            TAUserOptionLog(txtEmailFriend3.Text, txtFriendName3.Text);
                        }

                        if (txtEmailFriend4.Text.Trim() != "")
                        {
                            SendMail(txtEmailFriend4.Text, txtFriendName4.Text);
                            TAUserOptionLog(txtEmailFriend4.Text, txtFriendName4.Text);
                        }

                        if (txtEmailFriend5.Text.Trim() != "")
                        {
                            SendMail(txtEmailFriend5.Text, txtFriendName5.Text);
                            TAUserOptionLog(txtEmailFriend5.Text, txtFriendName5.Text);
                        }

                        Response.Redirect("email-usermessage.aspx");
                    }
                }
                else
                {
                    lblMessage.Text = "Wrong security check!";
                    CreateUpdateImageAndSession();
                }
            }
            else
            {
                lblMessage.Text = "Security Check code was expired. Please try again.";
                CreateUpdateImageAndSession();
            }
        }

    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        //Response.Redirect("~/ace/promo.aspx");
    }

    private bool SendMail(string FriendEmail, string FriendName)
    {
        bool _retVal = false;

        string _emailTo = FriendEmail;
        string _customername = txtFullName.Text;
        string _emailSubject = "Travel Insurance Promotion on CompareXpress";
        string _mailBody = "";
        string _currentDomainUrl = "http://" + HttpContext.Current.Request.Url.Host.ToString();
        string imgLogoUrl = _currentDomainUrl + CE.Configuration.MyConfigurationManager.GetAppSettingValue("LogoPath");
        string promoUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TravelPromoEDM").ToString();

        _mailBody = "<body class='email_body'>";       
        //_mailBody += "<center>";

        _mailBody += "Dear <strong>" + FriendName + ",</strong><br/><br/>";
        _mailBody += _customername + " forwards you this promotion on our website.<br/><br/>";
        _mailBody += "Kind regards,<br/><br/> <a href='http://www.comparexpress.com'>www.CompareXpress.com</a> <br/><br/> ";
        _mailBody += "<a href='http://www.comparexpress.com/ace/promo.aspx' target='_blank'>";
        _mailBody += "<img src='" + promoUrl + "' alt='CompareXpress' border='0' width='583' height='825' /></a><br/><br/>";

        _mailBody += "<hr />";
        _mailBody += "<br /><i>This is a computer-generated email. Please do not reply to this email.</i>";
        _mailBody += "<hr />";
        //_mailBody += "</center>";
        _mailBody += "</body>";

        if (MyMailManager.SendCRMMessage(_emailTo, _emailSubject, _mailBody))
        {
            _retVal = true;
        }
        else
        {
            _retVal = false;
        }

        return _retVal;
    }

    private void TAUserOptionLog(string FriendEmail, string FriendName)
    {
        TAUserOption tauseroption = new TAUserOption();
        TAUserOptionDB tauseroptiondb = new TAUserOptionDB();

        tauseroption.TAUserOptionId = 0;
        tauseroption.TAId = 0;
        tauseroption.IsBuyNow = false;
        tauseroption.IsCallMe = false;
        tauseroption.IsEmailMe = false;
        tauseroption.IsEmailThis = true;
        tauseroption.RecipientName = General.ClearScriptTag(FriendName);
        tauseroption.RecipientEmailAddress = FriendEmail;
        tauseroption.ViewedDetailId = 0;
        tauseroption.Ninfo1 = 36; //providerid

        if (Session[General.UserId] != null)
        {
            if (Session[General.UserId].ToString() != "")
                tauseroption.RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());
            else
                tauseroption.RefCEUserId = 0;
        }
        else
        {
            tauseroption.RefCEUserId = 0;
        }

        tauseroptiondb.Insert(tauseroption);
    }

    protected void btnNewImage_Click(object sender, EventArgs e)
    {
        CreateUpdateImageAndSession();
    }

    private void CreateUpdateImageAndSession()
    {
        MyImage myimage = new MyImage();
        string salt = "";

        if (Session[CE.Global.General.SecurityCheckWord] != null)
        {
            salt = myimage.GetRandomWord(Session[CE.Global.General.SecurityCheckWord].ToString());
        }
        else
        {
            if (Session[CE.Global.General.SecurityCheckWord] != null)
            {
                salt = myimage.GetRandomWord(Session[CE.Global.General.SecurityCheckWord].ToString());
            }
            else
            {
                salt = myimage.GetRandomWord("all");
            }
        }

        Session[CE.Global.General.SecurityCheckWord] = salt;
        imgCAPTCHA.ImageUrl = @"~/CAPTCHA/CAPTCHAImageHandler.ashx?id=" + CE.Security.MySecurity.Encrypt(salt, true);
        txtSecurityCode.Text = "";
    }
}
